/**
 * @author junwei
 * @date 16-7-23
 * 
 * 实现导出excel表 POI
 */
package com.cntcdt.api.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.cntcdt.api.db.model.GroupDeviceDayDivideCntDemoModel;
import com.cntcdt.api.db.model.GroupDeviceDayDivideCntModel;

public class CreateBorrowElectricityExcelToDisk {
	public static String fileName = "";

	public static void createExcel(List<GroupDeviceDayDivideCntModel> list) {
		// 创建一个webbook，对应一个Excel文件
		HSSFWorkbook wb = new HSSFWorkbook();
		// 在webbook中添加一个sheet,对应Excel文件中的sheet
		HSSFSheet sheet = wb.createSheet("来电科技");
		// 在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
		HSSFRow row = sheet.createRow((int) 0);
		// 创建单元格，并设置值表头 设置表头居中
		HSSFCellStyle style = wb.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

		HSSFCell cell = row.createCell((short) 0);
		cell.setCellValue("日期");
		cell.setCellStyle(style);
		cell = row.createCell((short) 1);
		cell.setCellValue("借电人数");
		cell.setCellStyle(style);
		cell = row.createCell((short) 2);
		cell.setCellValue("借电次数");
		cell.setCellStyle(style);
		cell = row.createCell((short) 3);
		cell.setCellValue("购线人数（含免费）");
		cell.setCellStyle(style);
		cell = row.createCell((short) 4);
		cell.setCellValue("新增用户");
		cell.setCellStyle(style);

		// 写入实体数据
		for (int i = 0; i < list.size(); i++) {
			row = sheet.createRow((int) i + 1);
			GroupDeviceDayDivideCntModel model = list.get(i);
			// 创建单元格，并设置值
			row.createCell((short) 0).setCellValue(model.getAddTime().toString());
			row.createCell((short) 1).setCellValue(model.getBorrowUserSum());
			row.createCell((short) 2).setCellValue(model.getBorrowCDBSum());
			row.createCell((short) 3).setCellValue(model.getSellLineNum());
			row.createCell((short) 4).setCellValue(model.getNewUserSum());
		}

		// 将文件存到指定位置
		FileOutputStream fout = null;
		try {
			File file = new File("./static/download/laidian_report_" + System.currentTimeMillis() + ".xls");
			fout = new FileOutputStream(file);

			fileName = file.getName();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			wb.write(fout);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			fout.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
